﻿using System.ComponentModel.Composition;
using System.Text;
using SS.Net.AppGen.Generators;
using SS.Net.AppGen.Models;
using SS.Net.AppGen.Utils;
using SS.Common.DBBuilder.DBDataStructure;

namespace SS.Net.AppGen.WebGenerators.WebControllerGenerator
{
    /// <summary>
    /// The algorithm for generating Insert method of user controller
    /// </summary>
    [Export(typeof(InsertControllerItemByUserAlgorithm))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class InsertControllerItemByUserAlgorithm : BaseGenerator, IInsertControllerItemAlgorithm
    {
        /// <summary>
        /// The method to generate Insert contents of user controller
        /// </summary>
        /// <param name="item">The item which extracted from template file</param>
        /// <param name="tbl">The datasource for generating</param>
        /// <returns></returns>
        public string GenerateContents(Item item, Table tbl)
        {
            var contents = new StringBuilder(BaseConsts.MAX_BYTES);

            contents.Append(BaseConsts.TAB3).AppendLine("var userName = Forms.Get(\"UserName\");");
            contents.Append(BaseConsts.TAB3).AppendLine("var password = Forms.Get(\"Password\");");
            contents.Append(BaseConsts.TAB3).AppendLine("var email = Forms.Get(\"Email\");");
            contents.Append(BaseConsts.TAB3).AppendLine("var role = Forms.Get(\"Role\");");

            foreach (Attribute attr in tbl.Attributes)
            {
                if (!attr.IsIdentify)
                {
                    contents.Append(BaseConsts.TAB3).AppendLine(string.Format("var {0} = Forms.Get(\"{1}\");",
                                                                        attr.Name.ToLowerFirstCharacter(), attr.Name));
                }
            }

            contents.Append(BaseConsts.TAB3).AppendLine(string.Empty);
            contents.Append(BaseConsts.TAB3).AppendLine(string.Format("var item = new Custom{0}", tbl.Name));
            contents.Append(BaseConsts.TAB3).AppendLine("{");

            contents.Append(BaseConsts.TAB4).AppendLine(string.Format("{0} = new {1}", tbl.Name, tbl.Name));
            contents.Append(BaseConsts.TAB8).AppendLine("{");

            contents.Append(BaseConsts.TAB9).AppendLine("UserName = userName,");
            contents.Append(BaseConsts.TAB9).AppendLine("Password = password,");
            contents.Append(BaseConsts.TAB9).AppendLine("Email = email,");
            contents.Append(BaseConsts.TAB9).AppendLine("Role = role,");

            contents.Append(BaseConsts.TAB8).AppendLine("}");

            

            //contents.Append(BaseConsts.TAB4).AppendLine(string.Format("{0} = new {1}", tbl.Name, tbl.Name));
            //contents.Append(BaseConsts.TAB4).AppendLine("{");

            //foreach (Attribute attr in tbl.Attributes)
            //{
            //    if (!attr.IsIdentify)
            //    {
            //        contents.Append(BaseConsts.TAB5).AppendLine(string.Format("{0} = {1},", attr.Name,
            //                                                                  this.BuildParsingData(attr)));
            //    }
            //}

            //contents.Append(BaseConsts.TAB4).AppendLine("}");
            contents.Append(BaseConsts.TAB3).AppendLine("};");

            return contents.ToString().TrimStart().TrimEnd();
        }
    }
}
